﻿using System;

namespace Platform.DB
{
    public sealed class DbFactory
    {
        #region 构造

        private DbFactory()
        {
        }

        #endregion

        #region 字段属性

        private static volatile IDbFactory _singleDbFactory = null;

        private static readonly object syncObj = new object();

        public static DBType dbType = DBType.odbc;

        #endregion

        #region 共有方法

        public static IDbFactory GetInstance()
        {
            try
            {
                string value = "mysql";
                dbType = (DBType)Enum.Parse(typeof(DBType), value, true);
            }
            catch
            {
            }
            if (_singleDbFactory == null)
            {
                object obj = syncObj;
                lock (obj)
                {
                    if (_singleDbFactory == null)
                    {
                        _singleDbFactory = CreateInstance();
                    }
                }
            }
            return _singleDbFactory;
        }

        #endregion

        #region 私有方法

        private static IDbFactory CreateInstance()
        {
            IDbFactory result = null;
            switch (dbType)
            {
                case DBType.odbc:
                    result = new OdbcFactory();
                    break;
                case DBType.mysql:
                    result = new MySqlFactory();
                    break;
            }
            return result;
        }

        #endregion

    }
}
